package com.ccsu.joysport.controller;

import com.ccsu.joysport.controller.model.resp.ActivityEntry;
import com.ccsu.joysport.controller.model.resp.ActivityResp;
import com.ccsu.joysport.dto.*;
import com.ccsu.joysport.entity.Activity;
import com.ccsu.joysport.service.ActivityService;
import com.ccsu.joysport.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("/ccsu/c/student/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 活动列表 - 分页
     */
    @GetMapping("/page")
    public ActivityResp clubPage(@RequestParam String token,
                                 @RequestParam(required = false) String searchText,
                                 @RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize) {
        if (!jwtUtil.validateToken(token)) {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "无效的token");
        }

        return activityService.activityPage(searchText, pageNum - 1, pageSize);
    }

    //查询单个活动信息
    @GetMapping("/queryActivityDetail")
    public ResponseEntity<?> queryActivityDetail(@RequestParam String token, @RequestParam String activityId) {
        if (!jwtUtil.validateToken(token)) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        //返回报名状态
        FormattedActivityResponse activity = activityService.getActivityDetail(token,activityId);
        return new ResponseEntity<>(activity,HttpStatus.OK);

    }

    //报名活动
    @PostMapping(value = "/signupActivity", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> signupActivity(@RequestBody SignupActivityRequest request) {
        if (!jwtUtil.validateToken(request.getToken())) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        SignupActivityResponse response = activityService.signupActivity(request);
        return new ResponseEntity<>(response, HttpStatus.OK);

    }

    //取消报名
    @PostMapping("/cancelSignupActivity")
    public ResponseEntity<?> cancelSignupActivity(@RequestBody CancelSignupActivityRequest request) {
        if (!jwtUtil.validateToken(request.getToken())) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        CancelSignupActivityResponse response = activityService.cancelSignupActivity(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //查看已参加活动
    @GetMapping("/viewJoinedActivities")
    public ResponseEntity<?> viewJoinedActivities(@RequestParam String token) {
        if (!jwtUtil.validateToken(token)) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        List<JoinedActivitiesResponse> activities = activityService.getJoinedActivities(token);
        return new ResponseEntity<>(activities, HttpStatus.OK);
    }

    //查询活动
    @GetMapping("/searchActivities")
    public ResponseEntity<?> searchActivities(@RequestParam String token, @RequestParam String queryText) {
        if (!jwtUtil.validateToken(token)) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        List<ActivityWithClubResponse> activities = activityService.searchActivities(queryText);
        return new ResponseEntity<>(activities, HttpStatus.OK);

    }
}
